package com.exmaple.lemlin.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.exmaple.lemlin.R;
import com.exmaple.lemlin.entity.ChatRecord;
import com.exmaple.lemlin.entity.FriendGood;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.UserViewHolder> implements View.OnClickListener {

    private Context mContext;
    private List<ChatRecord> records;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public ChatAdapter(Context mContext, List<ChatRecord> records) {
        this.mContext = mContext;
        this.records = records;
    }

    @NonNull
    @Override
    public UserViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_chat, viewGroup, (false));
        inflate.setOnClickListener(this);
        return new UserViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final UserViewHolder viewHolder, int position) {
        viewHolder.tvName.setText(records.get(position).getName());
        int count = records.get(position).getLastMsgCount();
        if (count == 0) {
            viewHolder.tvCount.setVisibility(View.GONE);
        } else {
            viewHolder.tvCount.setVisibility(View.VISIBLE);
            viewHolder.tvCount.setText(String.valueOf(count));
        }
        viewHolder.tvMsgText.setText(records.get(position).getLastMsgText());
        SimpleDateFormat sdf = new SimpleDateFormat(("MM-dd HH:mm"), Locale.getDefault());
        viewHolder.tvTime.setText(sdf.format(new Date(records.get(position).getLastMsgTime())));
        viewHolder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return records.size();
    }

    /**
     * 加载数据
     */
    public void addData(List<ChatRecord> records) {
        this.records.clear();
        this.records.addAll(records);
        notifyDataSetChanged();
    }

    /**
     * 加载数据
     */
    public void addItem(ChatRecord record) {
        this.records.add(getItemCount(), record);
        notifyItemChanged(getItemCount());
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null)
            onItemClickListener.onItemClick(v, (int) v.getTag());
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);
    }

    class UserViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_msg_text)
        TextView tvMsgText;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_count)
        TextView tvCount;

        UserViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind((this), itemView);
        }
    }
}
